package com.jay.media.music.ui.music

import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
import com.jay.base.ui.BaseFragment
import com.jay.media.kit.LogHelper
import com.jay.media.music.ui.HomeActivity
import com.jay.media.music.ui.R
import com.jay.media.music.ui.base.FragmentBackHandler
import com.jay.media.music.ui.databinding.FragmentMusicLibraryBinding

/**
 * @author xuzhou4
 * @date 2022/3/22
 */
class MusicLibraryFragment : BaseFragment(), FragmentBackHandler {

    private var _binding: FragmentMusicLibraryBinding? = null
    private val binding get() = _binding!!
    private lateinit var musicLibraryCategory: Array<String>
    private lateinit var musicLibraryCategoryId: Array<String>
    private var currentPosition = 0
    override fun rootView(container: ViewGroup?): View? {
        LogHelper.d(TAG, "rootView() called")
        _binding = FragmentMusicLibraryBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

    override fun initView(view: View) {
        LogHelper.d(TAG, "initView() called")
        (mActivity as HomeActivity).setupToolbarNavBar(binding.toolbar)
        (mActivity as HomeActivity).updateToolbarTitle(getString(R.string.music_library))
    }

    override fun initData() {
        Log.d(TAG, "initData() called")
        musicLibraryCategory = resources.getStringArray(R.array.music_library_category)
        musicLibraryCategoryId = resources.getStringArray(R.array.music_library_category_id)
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.let {
                    currentPosition = tab.position
                    switchFragment(tab.position)
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }
        })
        for (s in musicLibraryCategory) {
            binding.tabLayout.addTab(binding.tabLayout.newTab())
        }
        for (i in musicLibraryCategory.indices) {
            binding.tabLayout.getTabAt(i)?.text = musicLibraryCategory[i]
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "onDestroyView() called")
        _binding = null
    }

    private fun switchFragment(position: Int) {
        var fragment =
            childFragmentManager.findFragmentByTag(musicLibraryCategoryId[position])
                ?: MusicFragment.newInstance(musicLibraryCategoryId[position])
        val transaction = childFragmentManager.beginTransaction()
        transaction.replace(R.id.container, fragment, musicLibraryCategoryId[position])
        transaction.commit()
    }

    override fun onBackPressed(): Boolean {
        val fragment =
            childFragmentManager.findFragmentByTag(musicLibraryCategoryId[currentPosition])
        return if (fragment is FragmentBackHandler) {
            fragment.onBackPressed()
        } else false
    }
}